//
//  VideoBuyView.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/2.
//

import UIKit

class VideoBuyView: UIView {
    let disposeBag = DisposeBag()
    var chooseDone: (() -> Void)?
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "生成视频将花费100钻石"
        label.font = UIFont.pingFangSCRegular(ofSize: 20)
        label.textColor = .white
        label.textAlignment = .center
        return label
    }()
    
    private lazy var mymoneyLb: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textAlignment = .center
        label.font = UIFont.pingFangSCRegular(ofSize: 15)
        label.textColor = Theme.color.text_d4
        return label
    }()
    
    private lazy var loseLb: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textAlignment = .center
        label.font = UIFont.pingFangSCRegular(ofSize: 15)
        label.textColor = Theme.color.text_d4
        return label
    }()
    
    private lazy var cancelButton: UIButton = {
        let button = UIButton()
        button.setTitle("取消", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.titleLabel?.font = UIFont.pingFangSCRegular(ofSize: 16)
        button.backgroundColor = UIColor(hexString: "#FFFFFF", transparency: 0.1)
        button.allRound(25)
        return button
    }()
    
    private lazy var confirmButton: UIButton = {
        let button = UIButton()
        button.setTitle("确认", for: .normal)
        button.setTitleColor(Theme.color.text_d1, for: .normal)
        button.titleLabel?.font = UIFont.pingFangSCRegular(ofSize: 16)
        button.backgroundColor = UIColor(hexString: "#45FFFF")
        button.allRound(25)
        return button
    }()
    
    private lazy var alertView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hexString: "#28272D")
        view.layer.cornerRadius = 10
        view.layer.masksToBounds = true
        return view
    }()
    
    init() {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        self.backgroundColor = UIColor(hexString: "#000000", transparency: 0.3)
        addSubview(alertView)
        bindEvent()
    }
    
    func bindEvent(){
        cancelButton.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let self = self else { return }
            UIView.animate(withDuration: 0.3, animations: {
                self.alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
                self.alpha = 0
            },completion: { _ in
                self.removeFromSuperview()
            })
        }).disposed(by: disposeBag)
        
        confirmButton.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let self = self else { return }
            UIView.animate(withDuration: 0.3, animations: {
                self.alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
                self.alpha = 0
            },completion: { _ in
                self.removeFromSuperview()
                self.chooseDone?()
            })
        }).disposed(by: disposeBag)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initViews(money:String, lose:String, title:String) {
        alertView.addSubviews([titleLabel, mymoneyLb, loseLb, cancelButton, confirmButton])
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(24)
        }
        
        mymoneyLb.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.leading.equalToSuperview().offset(10)
            make.trailing.equalToSuperview().offset(-10)
        }
        
        loseLb.snp.makeConstraints { (make) in
            make.top.equalTo(mymoneyLb.snp.bottom).offset(6)
            make.leading.equalToSuperview().offset(10)
            make.trailing.equalToSuperview().offset(-10)
        }
         
        cancelButton.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().offset(16)
            make.top.equalTo(loseLb.snp.bottom).offset(32)
            make.height.equalTo(50)
        }
        
        confirmButton.snp.makeConstraints { (make) in
            make.trailing.equalToSuperview().offset(-16)
            make.width.equalTo(cancelButton.snp.width)
            make.leading.equalTo(cancelButton.snp.trailing).offset(12)
            make.top.equalTo(loseLb.snp.bottom).offset(32)
            make.height.equalTo(50)
        }
        
        alertView.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(24)
            make.centerY.equalToSuperview()
            make.top.greaterThanOrEqualTo(titleLabel.snp.top).offset(-24)
            make.bottom.greaterThanOrEqualTo(confirmButton.snp.bottom).offset(24)
        }
        
        self.titleLabel.text = title
        
        alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
        UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
            self.alertView.transform = .identity
        }, completion: nil)
      
        self.mymoneyLb.attributedText = createAttributedText(tips: "已拥有：", amount: money)
        self.loseLb.attributedText = createAttributedText(tips: "本次消耗：", amount: lose)
    }
    
    
    func createAttributedText(tips:String, amount: String) -> NSAttributedString {
           // 创建 NSMutableAttributedString
           let attributedText = NSMutableAttributedString(string: tips)
           // 创建并添加图片
           if let image = UIImage(named: "icon_mine_diamond") { // 使用系统图标作为示例
               let imageAttachment = NSTextAttachment()
               imageAttachment.image = image
               imageAttachment.bounds = CGRect(x: 0, y: -3, width: 22, height: 18) // 调整图片大小和位置
               attributedText.append(NSAttributedString(attachment: imageAttachment))
           }
           // 创建并添加动态文字金额
        let amountText = NSAttributedString(string: " \(amount)", attributes: [.font: UIFont.pingFangSCRegular(ofSize: 16)])
           attributedText.append(amountText)
           return attributedText
       }
    
}
